<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-8-11
 * Time: 11:59
 */

namespace App\Member\Renderables;

use App\Models\UserBalanceLog;
use App\Models\WeekCheckout;
use App\Models\WeekProfitReturn;
use frontend\models\User;
use Illuminate\Contracts\Support\Renderable;
use Dcat\Admin\Admin;
use Carbon\Carbon;
use Cache;


class AdminUserRenderables implements Renderable
{

    public static $js = [
        'echarts/echarts/dist/echarts.js',
        //'tree/tree.js',
    ];


    public function teamScript()
    {
        list($trade, $agent) = $this->trade_to_agen_merge();
        $trades = collect(array_reverse(array_values(collect($trade)->pluck('profit_checkout')->toArray())));
        $agents = collect(array_reverse(array_values(collect($agent)->pluck('profit_checkout')->toArray())));
        $dates  = collect(array_values(collect($trade)->pluck('date')->sort()->toArray()));

        //dd($agents, $trades);

        $licaiqianbao = trans('withdrawal.fields.lcqbxxxx');
        $yonjiqianbao = trans('withdrawal.fields.yjqbxxxx');

        return <<<JS
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));

            // 指定图表的配置项和数据
            var option = {
                    title: {
                        text: ''
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    legend: {
                        top: 10,
                        data: ["$licaiqianbao", "$yonjiqianbao"]
                    },
                    // toolbox: {
                    //     feature: {
                    //         saveAsImage: {}
                    //     }
                    // },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: {$dates}
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [
                        {
                            name: "$licaiqianbao",//'投资产品',
                            type: 'line',
                            areaStyle: {},
                            data: {$trades}
                        },
                        {
                            name: "$yonjiqianbao",//'下级产品',
                            type: 'line',
                            areaStyle: {},
                            data: {$agents}
                        }
                    ]
                };

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
JS;
    }


    public function render()
    {
        // 在这里可以引入你的js或css文件
        Admin::js(static::$js);

        // 需要在页面执行的JS代码
        // 通过 Admin::script 设置的JS代码会自动在所有JS脚本都加载完毕后执行
        Admin::script($this->teamScript());

        return view('member.admin_user.echarts')->render();
    }


    /***
     * 代理产品
     * @return mixed
     */
    public function trade()
    {
        $user = Admin::user();

        $weekCheckout = WeekCheckout::where('user_id', $user->id)->orderByDesc('created_at')
            ->select([
                \DB::raw('SUM(profit_checkout) as profit_checkout'),
                \DB::raw('created_at as date'),
            ])->groupBy('date')->limit(10)->get();

        //dd($weekCheckout->toArray());
        return $weekCheckout;
    }


    /***
     * 下级产品
     * @return mixed
     */
    public function agent()
    {
        $user = Admin::user();

        $agent = WeekProfitReturn::where('user_id', $user->id)
            ->where('ismail', 2)
            ->orderByDesc('created_at')
            ->select([
                \DB::raw('SUM(zt_volume) as zt_volume'),
                \DB::raw('SUM(yj_volume) as yj_volume'),
                \DB::raw('SUM(zt_profit) as zt_profit'),
                \DB::raw('SUM(yj_profit) as yj_profit'),
                \DB::raw('SUM(tj_profit) as tj_profit'),
                \DB::raw('created_at as date'),
            ])->groupBy('date')->limit(10)->get();

        return $agent;
    }


    /***
     * 数据合并
     * @return array
     */
    public function trade_to_agen_merge()
    {
        $trade = $this->trade();
        $agent = $this->agent();

        if (count($trade) <= 0) {
            $aa                       = [];
            $aa[0]['profit_checkout'] = 0;
            $aa[0]['date']            = 0;

            $trade = collect($aa);
        }

        if (count($agent) <= 0) {
            $agent->profit_checkout = 0;
            $agent->date            = 0;

            $bb                       = [];
            $bb[0]['profit_checkout'] = 0;
            $bb[0]['date']            = 0;

            $agent = collect($bb, true);
        }


        $newTrade = [];
        foreach (meiWeek() as $k => $v) {
            foreach ($trade as $kk => $vv) {
                if (isset($vv->date)) {
                    $d = $vv->date;
                } else {
                    $d = $vv['date'];
                }
                if ($v == Carbon::parse($d)->toDateString()) {
                    $newTrade[$k]['profit_checkout'] = $vv->profit_checkout;
                    $newTrade[$k]['date']            = Carbon::parse($d)->toDateString();
                    break;
                } else {
                    $newTrade[$k]['profit_checkout'] = 0;
                    $newTrade[$k]['date']            = $v;
                }
            }
        }

        $newAgent = [];
        foreach (meiWeek() as $k => $v) {
            foreach ($agent as $kk2 => $vv2) {
                if (isset($vv2->date)) {
                    $dd = $vv2->date;
                } else {
                    $dd = $vv2['date'];
                }

                if ($v == Carbon::parse($dd)->toDateString()) {
                    $newAgent[$k]['profit_checkout'] = getNumberFormat($vv2->zt_volume + $vv2->yj_volume +
                        $vv2->zt_profit + $vv2->yj_profit +
                        $vv2->tj_profit, 4);
                    //echo $newAgent[$k]['profit_checkout'] . '<br>';
                    $newAgent[$k]['date'] = Carbon::parse($dd)->toDateString();
                    break;
                } else {
                    $newAgent[$k]['profit_checkout'] = 0;
                    $newAgent[$k]['date']            = $v;
                }
            }
        }

        return [$newTrade, $newAgent];
    }

}
